package com.lzm.ydpt.module.order.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonutils.SolveEditTextScrollClash;
import com.byt.framlib.commonwidget.NoScrollListview;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.switchbutton.SwitchButton;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lzm.ydpt.BaseColor;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.DeliveryTimeBean;
import com.lzm.ydpt.entity.GoodsBean;
import com.lzm.ydpt.entity.ValueBean;
import com.lzm.ydpt.module.order.activity.AddGratuityActivity;
import com.lzm.ydpt.module.order.activity.AddressBookActivity;
import com.lzm.ydpt.module.order.activity.CashierActivity;
import com.lzm.ydpt.module.order.view.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页下订单
 */
public class UserHomeFragment extends BaseFragment {

    public static final int GRATUITY_CODE = 0X01;
    public static final int ADDRESS_BOOK_ONE = 0X02;
    public static final int ADDRESS_BOOK_TWO = 0X03;

    @BindView(R.id.rl_user_home_title)
    RelativeLayout rl_user_home_title;
    @BindView(R.id.tv_user_mail_info)
    TextView tv_user_mail_info;
    @BindView(R.id.tv_user_mail_address)
    TextView tv_user_mail_address;
    @BindView(R.id.tv_user_mail_hint)
    TextView tv_user_mail_hint;

    @BindView(R.id.tv_user_delivery_info)
    TextView tv_user_delivery_info;
    @BindView(R.id.tv_user_delivery_address)
    TextView tv_user_delivery_address;
    @BindView(R.id.tv_user_delivery_hint)
    TextView tv_user_delivery_hint;

    @BindView(R.id.tv_goods_name)
    TextView tv_goods_name;
    @BindView(R.id.tv_delivery_time)
    TextView tv_delivery_time;
    @BindView(R.id.tv_select_gratuity)
    TextView tv_select_gratuity;

    @BindView(R.id.sb_person_sign_play)
    SwitchButton sb_person_sign_play;
    @BindView(R.id.tv_remarks_content)
    TextView tv_remarks_content;

    @BindView(R.id.tv_kilometers)
    TextView tv_kilometers;
    @BindView(R.id.tv_estimated_amount)
    TextView tv_estimated_amount;

    private static UserHomeFragment sFragment;
    private boolean isPerson = false;

    private BottomSheetDialog mGoodsDialog;
    private List<GoodsBean> mGoodsList = new ArrayList<>();
    private List<ValueBean> mValueList = new ArrayList<>();
    private GoodsBean selectGoodsBean = null;
    private ValueBean selectValueBean = null;

    private BottomSheetDialog mDeliveryTimeDialog;
    private List<DeliveryTimeBean> mTimeNowList = new ArrayList<>();
    private List<DeliveryTimeBean> mDeliveryTimeList = new ArrayList<>();
    private DeliveryTimeBean selectDeliveryTimeBean = null;

    private float mGratuity = 0f;

    private String remarks = "";

    private BottomSheetDialog mRemarksDialog;

    public static UserHomeFragment newsInstance() {
        sFragment = new UserHomeFragment();
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_user_home;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        sb_person_sign_play.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
            }
        });
    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }

    @OnClick({R.id.tv_mail_address_book, R.id.tv_delivery_address_book, R.id.tv_settlement, R.id.rl_select_goods, R.id.rl_delivery_time
            , R.id.rl_gratuity_amount, R.id.rl_remarks_info})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_mail_address_book:
                Bundle mail = new Bundle();
                mail.putInt(Constants.INP_APPEND_ADDRESS_TYPE, 1);
                openActivityForResult(AddressBookActivity.class, mail, ADDRESS_BOOK_ONE);
                break;
            case R.id.tv_delivery_address_book:
                Bundle delivery = new Bundle();
                delivery.putInt(Constants.INP_APPEND_ADDRESS_TYPE, 1);
                openActivityForResult(AddressBookActivity.class, delivery, ADDRESS_BOOK_TWO);
                break;
            case R.id.tv_settlement:
                openActivity(CashierActivity.class);
                break;
            case R.id.rl_select_goods:
                showGoodsDialog();
                break;
            case R.id.rl_delivery_time:
                showDeliveryTimeDialog();
                break;
            case R.id.rl_gratuity_amount:
                Bundle bundle = new Bundle();
                bundle.putFloat(Constants.INP_GRATUITY_VALUE, mGratuity);
                openActivityForResult(AddGratuityActivity.class, bundle, GRATUITY_CODE);
                break;
            case R.id.rl_remarks_info:
                showRemarksDialog();
                break;
        }
    }

    public void setGratuityValue(float gratuity) {
        this.mGratuity = gratuity;
        if (mGratuity > 0) {
            tv_select_gratuity.setText("¥ " + gratuity);
        } else {
            tv_select_gratuity.setText("");
        }
    }

    /**
     * 选择物品dialog
     */
    private void showGoodsDialog() {
        if (mGoodsDialog == null) {
            mGoodsDialog = new BottomSheetDialog(context);
            View view = getLayoutInflater().inflate(R.layout.layout_goods_select_sheet, null);
            TextView tv_reduce_goods = view.findViewById(R.id.tv_reduce_goods);
            TextView tv_goods_num = view.findViewById(R.id.tv_goods_num);
            TextView tv_increase_goods = view.findViewById(R.id.tv_increase_goods);
            ImageView img_goods_sheet_dismiss = view.findViewById(R.id.img_goods_sheet_dismiss);
            img_goods_sheet_dismiss.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    goodsDialogDismiss();
                }
            });
            TextView tv_commit_goods = view.findViewById(R.id.tv_commit_goods);
            tv_commit_goods.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    for (GoodsBean goodsBean : mGoodsList) {
                        if (goodsBean.isSelect()) {
                            selectGoodsBean = goodsBean;
                        }
                    }
                    for (ValueBean valueBean : mValueList) {
                        if (valueBean.isSelect()) {
                            selectValueBean = valueBean;
                        }
                    }
                    if (selectGoodsBean == null) {
                        showShortToast("请选择物品品类");
                        return;
                    }
                    if (selectValueBean == null) {
                        showShortToast("请选择物品价值");
                        return;
                    }
                    tv_goods_name.setText(selectGoodsBean.getGoods_name() + "  " + selectValueBean.getValue_name());
                    goodsDialogDismiss();
                }
            });

            tv_reduce_goods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectGoodsBean == null) {
                        showShortToast("请选择物品品类");
                        return;
                    }
                    if (selectGoodsBean.getGoods_count() == 1) {
                        showShortToast("不能更少了");
                        return;
                    }
                    selectGoodsBean.setGoods_count(selectGoodsBean.getGoods_count() - 1);
                    tv_goods_num.setText(String.valueOf(selectGoodsBean.getGoods_count()));
                }
            });

            tv_increase_goods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectGoodsBean == null) {
                        showShortToast("请选择物品品类");
                        return;
                    }
                    selectGoodsBean.setGoods_count(selectGoodsBean.getGoods_count() + 1);
                    tv_goods_num.setText(String.valueOf(selectGoodsBean.getGoods_count()));
                }
            });

            LinearLayout llList = view.findViewById(R.id.ll_sku_list);//列表
            // 物品品类
            View viewMonth = getLayoutInflater().inflate(R.layout.layout_goods_type_sheet, null);
            TextView tv_month_title = viewMonth.findViewById(R.id.tv_sku_title);
            tv_month_title.setText("物品品类");
            RecyclerView recyclerMonth = viewMonth.findViewById(R.id.recycler_sku_bottom);
            mGoodsList.clear();
            mGoodsList.add(new GoodsBean(1L, "食品饮料", false));
            mGoodsList.add(new GoodsBean(2L, "鲜花", false));
            mGoodsList.add(new GoodsBean(3L, "蛋糕", false));
            mGoodsList.add(new GoodsBean(4L, "文件", false));
            mGoodsList.add(new GoodsBean(5L, "水果生鲜", false));
            mGoodsList.add(new GoodsBean(6L, "数码家电", false));
            mGoodsList.add(new GoodsBean(7L, "服装鞋帽", false));
            mGoodsList.add(new GoodsBean(8L, "其他", false));

            RvCommonAdapter<GoodsBean> mGoodsAdapter = new RvCommonAdapter<GoodsBean>(context, mGoodsList, R.layout.item_goods_type_layout) {
                @Override
                protected void convert(RvViewHolder holder, GoodsBean goodsBean, int position) {
                    holder.setText(R.id.tv_goods_type_name, goodsBean.getGoods_name());
                    holder.setSelected(R.id.tv_goods_type_name, goodsBean.isSelect());
                    holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            for (GoodsBean tempGoods : mGoodsList) {
                                tempGoods.setSelect(tempGoods.equals(goodsBean));
                            }
                            selectGoodsBean = goodsBean;
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            GridLayoutManager goodsManager = new GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false);
            recyclerMonth.setLayoutManager(goodsManager);
            recyclerMonth.setNestedScrollingEnabled(false);
            recyclerMonth.addItemDecoration(new GridSpacingItemDecoration(4, getResources().getDimensionPixelSize(R.dimen.x10), true));
            recyclerMonth.setAdapter(mGoodsAdapter);
            llList.addView(viewMonth);

            // 物品价值
            View viewMeal = getLayoutInflater().inflate(R.layout.layout_goods_type_sheet, null);
            TextView tv_meal_title = viewMeal.findViewById(R.id.tv_sku_title);
            tv_meal_title.setText("物品价值");
            RecyclerView recyclerMeal = viewMeal.findViewById(R.id.recycler_sku_bottom);
            mValueList.clear();
            mValueList.add(new ValueBean(1L, "50元以下", false));
            mValueList.add(new ValueBean(2L, "50-100元", false));
            mValueList.add(new ValueBean(3L, "100-500元", false));
            mValueList.add(new ValueBean(4L, "500-1000元", false));
            mValueList.add(new ValueBean(5L, "1000-5000元", false));
            mValueList.add(new ValueBean(6L, "5000-10000元", false));

            RvCommonAdapter<ValueBean> mValueAdapter = new RvCommonAdapter<ValueBean>(context, mValueList, R.layout.item_goods_type_layout) {
                @Override
                protected void convert(RvViewHolder holder, ValueBean valueBean, int position) {
                    holder.setText(R.id.tv_goods_type_name, valueBean.getValue_name());
                    holder.setSelected(R.id.tv_goods_type_name, valueBean.isSelect());
                    holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            for (ValueBean tempValue : mValueList) {
                                tempValue.setSelect(tempValue.equals(valueBean));
                            }
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            GridLayoutManager valueManager = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
            recyclerMeal.setLayoutManager(valueManager);
            recyclerMeal.setNestedScrollingEnabled(false);
            recyclerMeal.addItemDecoration(new GridSpacingItemDecoration(3, getResources().getDimensionPixelSize(R.dimen.x10), true));
            recyclerMeal.setAdapter(mValueAdapter);
            llList.addView(viewMeal);

            mGoodsDialog.setContentView(view);
            View parent = (View) view.getParent();//获取ParentView
            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
            view.measure(0, 0);
            behavior.setPeekHeight(view.getMeasuredHeight());
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
            params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
            parent.setLayoutParams(params);
            mGoodsDialog.setCancelable(false);
            mGoodsDialog.setCanceledOnTouchOutside(true);
            mGoodsDialog.show();
        } else {
            mGoodsDialog.show();
        }
    }

    private void goodsDialogDismiss() {
        if (mGoodsDialog != null && mGoodsDialog.isShowing()) {
            mGoodsDialog.dismiss();
        }
    }

    /**
     * 选择发货时间
     */
    private void showDeliveryTimeDialog() {
        if (mDeliveryTimeDialog == null) {
            mDeliveryTimeDialog = new BottomSheetDialog(context);
            View view = getLayoutInflater().inflate(R.layout.layout_delivery_time_sheet, null);
            TextView tv_commit_time = view.findViewById(R.id.tv_commit_time);
            tv_commit_time.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if (selectDeliveryTimeBean == null) {
                        showShortToast("请选择发货时间");
                        return;
                    }
                    DeliveryTimeBean deliveryTime = null;
                    for (DeliveryTimeBean tempTime : mTimeNowList) {
                        if (tempTime.isSelect()) {
                            deliveryTime = tempTime;
                        }
                    }
                    tv_delivery_time.setText((deliveryTime.getPosition() == 1 ? "" : deliveryTime.getTime_now() + " ") + selectDeliveryTimeBean.getTime_now());
                    deliveryTimeDialogDismiss();
                }
            });

            NoScrollListview rv_time_now = view.findViewById(R.id.rv_time_now);
            mTimeNowList.clear();
            mTimeNowList.add(new DeliveryTimeBean(1, "今天", true));
            mTimeNowList.add(new DeliveryTimeBean(2, "明天", false));
            mTimeNowList.add(new DeliveryTimeBean(3, "后天", false));

            RecyclerView rv_time_section = view.findViewById(R.id.rv_time_section);
            initDeliveryTime(mTimeNowList.get(0));
            RvCommonAdapter<DeliveryTimeBean> mDeliveryAdapter = new RvCommonAdapter<DeliveryTimeBean>(context, mDeliveryTimeList, R.layout.item_time_section_layout) {
                @Override
                protected void convert(RvViewHolder holder, DeliveryTimeBean deliveryTimeBean, int position) {
                    holder.setText(R.id.img_time_section_name, deliveryTimeBean.getTime_now() + (deliveryTimeBean.getAddPrice() > 0 ? "     加" + deliveryTimeBean.getAddPrice() + "元" : ""));
                    holder.setVisible(R.id.img_time_section_select, deliveryTimeBean.isSelect());
                    holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            for (DeliveryTimeBean tempTime : mDeliveryTimeList) {
                                tempTime.setSelect(tempTime.equals(deliveryTimeBean));
                            }
                            selectDeliveryTimeBean = deliveryTimeBean;
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            rv_time_section.setLayoutManager(new LinearLayoutManager(context));
            rv_time_section.setAdapter(mDeliveryAdapter);

            LvCommonAdapter<DeliveryTimeBean> mTimeNowAdapter = new LvCommonAdapter<DeliveryTimeBean>(context, mTimeNowList, R.layout.item_time_now_layout) {
                @Override
                protected void convert(LvViewHolder holder, DeliveryTimeBean timeNow, int position) {
                    holder.setText(R.id.tv_time_now_name, timeNow.getTime_now());
                    if (timeNow.isSelect()) {
                        holder.setSelected(R.id.tv_time_now_name, true);
                        holder.setBackgroundColor(R.id.tv_time_now_name, BaseColor.color_white);
                    } else {
                        holder.setSelected(R.id.tv_time_now_name, false);
                        holder.setBackgroundColor(R.id.tv_time_now_name, BaseColor.color_f1f1f1);
                    }
                    holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            for (DeliveryTimeBean tempTime : mTimeNowList) {
                                tempTime.setSelect(tempTime.equals(timeNow));
                            }
                            initDeliveryTime(timeNow);
                            mDeliveryAdapter.notifyDataSetChanged();
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            rv_time_now.setAdapter(mTimeNowAdapter);

            mDeliveryTimeDialog.setContentView(view);
            View parent = (View) view.getParent();//获取ParentView
            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
            view.measure(0, 0);
            behavior.setPeekHeight(view.getMeasuredHeight());
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
            params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
            parent.setLayoutParams(params);
            mDeliveryTimeDialog.setCancelable(true);
            mDeliveryTimeDialog.setCanceledOnTouchOutside(true);
            mDeliveryTimeDialog.show();
        } else {
            mDeliveryTimeDialog.show();
        }
    }

    private void deliveryTimeDialogDismiss() {
        if (mDeliveryTimeDialog != null && mDeliveryTimeDialog.isShowing()) {
            mDeliveryTimeDialog.dismiss();
        }
    }

    private void initDeliveryTime(DeliveryTimeBean timeBean) {
        mDeliveryTimeList.clear();
        Calendar cal = Calendar.getInstance();
        if (timeBean.getPosition() == 1) {
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);
            //多少个半小时
            int delTime = (24 - hour) * 2 + (minute < 30 ? 0 : 1);
            for (int i = 0; i < delTime; i++) {
                int AMinute = (minute + i * 30) % 60 > 30 ? 30 : 0;
                int AHour = hour + (minute + i * 30) / 60;

                int BMinute = (AMinute == 30 ? 0 : 30);
                int BHour = AMinute == 30 ? AHour + 1 : AHour;

                cal.set(Calendar.HOUR, AHour);
                cal.set(Calendar.MINUTE, AMinute);
                long deliveryTime = cal.getTimeInMillis() / 1000;
                mDeliveryTimeList.add(getDeliveryTime(1, i + 1, deliveryTime, AHour, AMinute, BHour, BMinute));
            }
        } else {
            if (timeBean.getPosition() == 2) {
                cal.add(Calendar.DATE, 1);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.MILLISECOND, 0);
            } else if (timeBean.getPosition() == 3) {
                cal.add(Calendar.DATE, 2);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.MILLISECOND, 0);
            }
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);
            //多少个半小时
            int delTime = (24 - hour) * 2 + (minute < 30 ? 0 : 1);

            for (int i = 0; i < delTime; i++) {
                int AHour = hour + i / 2;
                int AMinute = minute + i % 2 * 30;

                int BMinute = (AMinute == 30 ? 0 : 30);
                int BHour = AMinute == 30 ? AHour + 1 : AHour;

                cal.set(Calendar.HOUR, AHour);
                cal.set(Calendar.MINUTE, AMinute);
                long deliveryTime = cal.getTimeInMillis() / 1000;
                mDeliveryTimeList.add(getDeliveryTime(2, i + 1, deliveryTime, AHour, AMinute, BHour, BMinute));
            }
        }
    }

    private DeliveryTimeBean getDeliveryTime(int position, int index, long deliveryTime, int AHour, int AMinute, int BHour, int BMinute) {
        DeliveryTimeBean timeBean = new DeliveryTimeBean();
        if (AHour >= 22) {
            timeBean.setAddPrice(3f);
        } else if (AHour < 7) {
            timeBean.setAddPrice(6f);
        } else {
            timeBean.setAddPrice(0f);
        }
        timeBean.setDeliveryTime(deliveryTime);
        timeBean.setPosition(index);
        if (index == 1 && position == 1) {
            timeBean.setTime_now("现在");
            timeBean.setSelect(true);
            selectDeliveryTimeBean = timeBean;
        } else {
            timeBean.setTime_now(trimTimeStr(AHour) + ":" + trimTimeStr(AMinute) + " - " + trimTimeStr(BHour) + ":" + trimTimeStr(BMinute));
            timeBean.setSelect(false);
        }
        return timeBean;
    }

    private String trimTimeStr(int data) {
        if (data < 10) {
            return "0" + data;
        } else {
            return String.valueOf(data);
        }
    }

    /**
     * 填写备注dialog
     */
    private void showRemarksDialog() {
        if (mRemarksDialog == null) {
            mRemarksDialog = new BottomSheetDialog(context);
            View view = getLayoutInflater().inflate(R.layout.layout_remarks_sheet, null);
            EditText ed_remarks_content = view.findViewById(R.id.ed_remarks_content);
            TextView tv_remarks_hint = view.findViewById(R.id.tv_remarks_hint);
            ed_remarks_content.setOnTouchListener(new SolveEditTextScrollClash(ed_remarks_content));
            ed_remarks_content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    tv_remarks_hint.setText(s.length() + "/200");
                }
            });
            ImageView img_remarks_sheet_dismiss = view.findViewById(R.id.img_remarks_sheet_dismiss);
            img_remarks_sheet_dismiss.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    remarksDialogDismiss();
                }
            });
            TextView tv_commit_remarks = view.findViewById(R.id.tv_commit_remarks);
            tv_commit_remarks.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    remarks = ed_remarks_content.getText().toString();
                    tv_remarks_content.setText(remarks);
                    goodsDialogDismiss();
                }
            });
            mRemarksDialog.setContentView(view);
            View parent = (View) view.getParent();//获取ParentView
            BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
            view.measure(0, 0);
            behavior.setPeekHeight(view.getMeasuredHeight());
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
            params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
            parent.setLayoutParams(params);
            mRemarksDialog.setCancelable(false);
            mRemarksDialog.setCanceledOnTouchOutside(true);
            mRemarksDialog.show();
        } else {
            mRemarksDialog.show();
        }
    }

    private void remarksDialogDismiss() {
        if (mRemarksDialog != null && mRemarksDialog.isShowing()) {
            mRemarksDialog.dismiss();
        }
    }
}